/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pqno;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author wida46984148
 */
public class TopDownMovement extends Component {

    float direction;
    float speed;
    boolean pause = true;

    public TopDownMovement(String id) {
        this.id = id;
    }

    public float getSpeed() {
        return speed;
    }

    public float getDirection() {
        return direction;
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sb, int delta) {

        float rotation = owner.getRotation();
        float scale = owner.getScale();
        Vector2f position = owner.getPosition();

        Input input = gc.getInput();

        if (input.isKeyDown(Input.KEY_A)) {
            float auxX = position.x;
            position.x += -0.4f * delta;
            if (position.x <= -30) {
                position.x = auxX;
            }
        }

        if (input.isKeyDown(Input.KEY_D)) {
            float auxX = position.x;
            position.x += 0.4f * delta;
            if (700 <= position.x) {
                position.x = auxX;
            }
        }

        if (input.isKeyDown(Input.KEY_W)) {
            float hip = 0.4f * delta;
            float auxY = position.y;
            position.x += hip * java.lang.Math.sin(java.lang.Math.toRadians(rotation));
            position.y -= hip * java.lang.Math.cos(java.lang.Math.toRadians(rotation));
                System.out.println(rotation);
            if (position.y <= 0) {
                position.y = auxY;
            }
        }
        if (input.isKeyDown(Input.KEY_S)) {
            float hip = 0.4f * delta;
            float auxY = position.y;
                System.out.println(rotation);
            position.x += hip * java.lang.Math.sin(java.lang.Math.toRadians(rotation));
            position.y += hip * java.lang.Math.cos(java.lang.Math.toRadians(rotation));
            if (500 <= position.y) {
                position.y = auxY;
            }
        }
        /*
         *
         * PAUSE
         *
         */

        if (pause) {
            if (input.isKeyPressed(Input.KEY_P)) {
                gc.pause();
                pause = false;
            }
        } else {
            if (input.isKeyPressed(Input.KEY_P)) {
                gc.resume();
                pause = true;
            }
        }

        owner.setPosition(position);
        owner.setRotation(rotation);
        owner.setScale(scale);
    }
}
